import axios from 'axios';
import router from '@/router';

axios.defaults.baseURL = '/api/'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// http request 拦截器
axios.interceptors.request.use(

    config => {
      if (localStorage.token) { //判断token是否存在
        config.headers.Authorization = localStorage.token;  //将token设置成请求头
      }
      console.log(config,config.headers)
      return config;
    },
    err => {
      return Promise.reject(err);
    }
);

// http response 拦截器
axios.interceptors.response.use(
    response => {
      if (response.data.errno === 999) {
        router.replace('/');
        console.log("token过期");
      }
      return response;
    },
    error => {
      return Promise.reject(error);
    }
);
export default axios;
